<?php

global $CFG;
require_once('base.php');
require_once( $CFG->dirroot . 'mod/folio/model/comment.php');


class folio_CommentBox extends folio_BaseView {

    private $page;
    private $pre = 'folio_CommentBox';

    /**
     * Create a comment box for the page.  Only returns content for
     *  logged in users.
     *
     * @param folio_page $page Page on which the comment will be left
     */
    function __construct( folio_page $page ) {

        if ( isloggedin() ) {
            $this->page = $page;
            $this->html = $this->_html();
            $this->js = $this->_js();
            $this->metatags = $this->_metatags();

            $this->Load( new folio_Tooltip("{$this->pre}private_div",
                _gettext('Don\'t save the comment. <br/> ' .
                         'Instead, you and the page owner <br/>' .
                         '(or last editor if a community page) <br/>' .
                         'will received an emailed copy.')));
        }
    }

    private function _html( ) {
        $oTitle = __gettext('Post a new Comment');

        $html = <<< END
<h2>{$oTitle}</h2>
<form name="{$this->pre}form">
    <div id="{$this->pre}div"></div>
    <textarea name="{$this->pre}comment" rows=2 style="
        border: 1px solid #7F9DB9;
        color:#71717B;
        width: 95%;
        padding:3px;" id="{$this->pre}comment" ></textarea>
    <br/>
    <div id="{$this->pre}private_div">
        <input type=checkbox id='{$this->pre}private'/>Make comment private<br/>
    </div>
    <input type="button" value="Post" onClick="{$this->pre}makeRequest();" />
</form>
END;
//        <input id="ysearchinput" type="text">

        return $html;
    }

    /**
     * Return the js for the control
     *
     * @param folio_page $page
     * @return string
     */
    private function _js( ) {
        $url = url . 'mod/folio/ajax/comment_insert.php';
        // Note: need the \/ to escape the js value.
        $oFailure = '<b>' . __gettext('Failure saving content, please try again') . '<\/b>';
        $oSuccess = '<b>' . __gettext('Comment posted!') . '<\/b>';
        $ajaxprefix = $this->pre;

        $js = <<< JS
var {$ajaxprefix}div = document.getElementById('{$ajaxprefix}div');

var {$ajaxprefix}handleSuccess = function(o){
    if(o.responseText !== undefined && o.responseText == 'true'){
        {$ajaxprefix}div.innerHTML = '{$oSuccess}';
        document.{$ajaxprefix}form.{$ajaxprefix}comment.value='';
    } else {
        {$ajaxprefix}div.innerHTML = '{$oFailure}';
    }
};

var {$ajaxprefix}handleFailure = function(o){
    if(o.responseText !== undefined){
        {$ajaxprefix}div.innerHTML = o.responseText;
    }
};

var {$ajaxprefix}callback =
{
  success:{$ajaxprefix}handleSuccess,
  failure:{$ajaxprefix}handleFailure,
  argument:['foo','bar']
};

function {$ajaxprefix}makeRequest(){
    var postData = "page_ident={$this->page->page_ident}&private=" +
        document.{$ajaxprefix}form.{$ajaxprefix}private.checked +
        "&body=" +
        escape( document.{$ajaxprefix}form.{$ajaxprefix}comment.value );
    var request = YAHOO.util.Connect.asyncRequest('POST', "{$url}", {$ajaxprefix}callback, postData);
}
JS;

        return $js;
    }


    /**
     * Return the js libraries
     *
     * @return array of filenames (no url) local to the folio plug-in.
     */
    private function _metatags() {
        return array(
            'mod/folio/lib/yui_2.4.1/build/yahoo-dom-event/yahoo-dom-event.js',
            'mod/folio/lib/yui_2.4.1/build/connection/connection-min.js'
                    );
    }
}

class folio_CommentViewer extends folio_BaseView {

    private $page;
    private $pre = 'folio_CommentViewer';

    /**
     * Show comments for the page.
     *
     * @param folio_page $page
     */
    function __construct( folio_page $page ) {

        $this->page = $page;
        $this->html = $this->_html();
    }

    private function _html( ) {
        global $FOLIO_CFG;

        $oTitle = __gettext('Comments');
        $url = url;
        $comments = folio_comment::SelectWherePage( $this->page );
        $html = '';
        $delete = '';
        $oDelete = __gettext('Delete');

        // Do we have permission to delete the comment?
        if( isloggedin() AND $this->page->Permission('update') ) {
            $delete = true;
        }

        if( $comments != false AND count($comments) > 0 ) {

            foreach( $comments as $comment ) {
                $body = htmlspecialchars(stripslashes($comment->body));
                $postedbylink = "<a href='{$url}" .
                    "mod/folio/html/user_redirection.php?ident={$comment->owner}'" .
                    ">{$comment->postedname}</a>";
                $posted = date($FOLIO_CFG->gmstyle, intval($comment->posted));

                if( $delete != '' )
                    $delete = "<a href='{$url}mod/folio/html/delete_comment.php?ident=" .
                        "{$comment->ident}&page_ident={$this->page->page_ident}'>({$oDelete})</a></p>";

                $html .= <<< END
<div class="infoholder">
    <p style="border: 1px solid #7F9DB9; width: 95%; padding:3px;">
    {$body}<br/>
    <span style="color:#71717B; ">&nbsp;&nbsp;&nbsp;
        <i>{$postedbylink} - {$posted}</i>
    </span> {$delete}
</div>
END;
            }

            $html = <<< END
<h2>{$oTitle}</h2>
{$html}
END;

        }

        return $html;
    }
}

?>
